package com.example.crawler.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * Java 通过EasyExcel导出的Excel文档的字体，背景色，自动列宽等符合要求
 */
public class EasyExcelUtil {

    public void write(List list, Class classType, String outPutPath) throws FileNotFoundException {
        // 文件输出位置
//        OutputStream out = new FileOutputStream("/Users/quanlinglong/Downloads/mergeDemo/demo" + System.currentTimeMillis() + ".xlsx");
        OutputStream out = new FileOutputStream(outPutPath);
        EasyExcel.write(out, classType).needHead(true)
//                .head(head()) //表头
                .excelType(ExcelTypeEnum.XLSX)
                .registerWriteHandler(new ExcelWidthStyleStrategy()) // 设置宽度
                .sheet(0, "拟上市企业数据")
                .doWrite(list); // 内容
    }

//    private List<List<String>> head() {
//        List<List<String>> headTitles = Lists.newArrayList();
//        headTitles.add(Lists.newArrayList("最新发布日期"));
//        headTitles.add(Lists.newArrayList("申请人"));
//        return headTitles;
//    }

}

